//
//  WYMUserAccount.swift
//  iOS11weibo
//
//  Created by 王应明 on 16/7/30.
//  Copyright © 2016年 DescendantsOftheSun. All rights reserved.
//

import UIKit

///用户账户模型
class WYMUserAccount: NSObject,NSCoding {
    
    /// AccessToken,用于获取用户数据
    var access_token: String?
    
    ///使用kvc属性不要用private如果要加就要在前面加@objc
    /// access_token的生命周期，单位是秒数。
    ///基本数据类型kvc找不到属性
    var expires_in: NSTimeInterval = 0 {
        didSet {
            
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    ///过去时间
    var expiresDate: NSDate?
    
    /// 授权用户的UID，
    var uid: String?
    
    
    
    init(dict: [String: AnyObject]) {
        //        access_token = dict["access_token"] as! String
        //        expires_in = dict["expi res_in"] as! NSTimeInterval
        //        uid = dict["uid"] as! String
        
        super.init()
        //对象方法需要爱对象创建完成后才能调用
        setValuesForKeysWithDictionary(dict)
        
    }
    
    //字典中的key在模型中找不到对应的属性，如果不实现就会崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String {
        get {
            return"access_token:\(access_token),expires_in:\(expires_in),uid:\(uid),expiresDate:\(expiresDate)"
        }
    }
    
    ///MARK--归档和解档相关
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
       expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        super.init()
    }
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
    }
    
}
